package com.canbot.u05.activity.vipguide;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.UsherModeRecordDialog;
import com.canbot.u05.view.UsherModeXFListenerDialog;

import java.util.HashMap;

/**
 * Created by cwf on 2017/5/26.
 * 贵宾引导文字转声音界面
 */

public class VipGuideTextToVoiceActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private EditText ettext2voice;

        private UsherModeXFListenerDialog usherModeRecordDialog;

        private String isSetting;

        private String identification;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.vipguide_text_to_voice);
                setViews();
                isSetting = getIntent().getStringExtra("isSetting");
                identification=getIntent().getStringExtra("identification");
        }

        private void setViews() {
                findViewById(R.id.imageView_back).setOnClickListener(this);
                findViewById(R.id.iv_text2voice_cancle).setOnClickListener(this);
                findViewById(R.id.iv_text2voice_sure).setOnClickListener(this);
                findViewById(R.id.rl_activity).setOnClickListener(this);
                findViewById(R.id.iv_text2voice_voice_iocn).setOnClickListener(this);
                findViewById(R.id.ll_voice_record).setOnClickListener(this);

                ettext2voice = (EditText) findViewById(R.id.et_text2voice);
                if(TextUtils.isEmpty(isSetting)){
                        ettext2voice.setText(getIntent().getStringExtra("content"));
                }
                ettext2voice.addTextChangedListener(watcher);
                ettext2voice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View view, boolean b) {
                                ettext2voice.setSelection(ettext2voice.length());
                        }
                });
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 500) {
                                ettext2voice.setText(s.subSequence(0, 500));
                                ettext2voice.setSelection(500);
                                ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.input_up_words2));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);

                                break;
                        case R.id.rl_activity: //点击et外面隐藏输入框
                                InputMethodManager imm = (InputMethodManager)
                                        getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                                break;
                        case R.id.iv_text2voice_sure://todo 确认
                                //确定
                                String trim = ettext2voice.getText().toString().trim();
                                if (TextUtils.isEmpty(trim)) {
                                        ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.text_cannot_empty));
                                        return;
                                }


                                if (!TextUtils.isEmpty(isSetting)) {
                                        showHint(trim);
                                        startActivity(new Intent(VipGuideTextToVoiceActivity.this, ReceptionSettingActivity.class)
                                                .putExtra("isSetting", isSetting)
                                                .putExtra("identification", identification));
                                }
                                else {
                                        showHint(trim);
                                        MainApplication.finishActivity(WelcomeSettingActivity.class.getName());
                                }
                                finish();
                                break;
                        case R.id.iv_text2voice_cancle://取消
                                finish();
                                break;
                        case R.id.iv_text2voice_voice_iocn: //点击声音图标按钮
                                String ettext = ettext2voice.getText().toString().trim();
                                if (TextUtils.isEmpty(ettext)) {
                                        ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.text_cannot_empty));
                                        return;

                                }
                                sendMsg(0, ettext);
                                break;
                        case R.id.ll_voice_record://点击语音录入
                                if (usherModeRecordDialog == null) {
                                        //点击语音录入图标
                                        usherModeRecordDialog = new UsherModeXFListenerDialog(this, UsherModeRecordDialog.USHER_MODE);
                                        usherModeRecordDialog.setFinishClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        usherModeRecordDialog.ok();
                                                }
                                        });
                                        usherModeRecordDialog.setCancleClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        usherModeRecordDialog.no();
                                                }
                                        });
                                        setDialogWindowAttr(usherModeRecordDialog, this);
                                }
                                usherModeRecordDialog.show(xunFeiResult);
                                break;
                        default:
                                break;
                }

        }

        public void showHint(String content) {
                HashMap<String, Object> paramsMap = new HashMap<String, Object>();

                //音频
                paramsMap.put("vipType", "updateRoute");
                paramsMap.put("identification", getIntent().getStringExtra("identification"));
                paramsMap.put("voicetype", 1 + "");
                paramsMap.put("content", content);//tts

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "routevipGuide", paramsMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });


        }

        private void sendMsg(int type, String ettext) {
                String url = IpMsgConst.HTTP_URL + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", type + "");
                hashMap.put("data", ettext);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                        }

                        @Override
                        public void onFailure() {

                                ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.network_error));
                        }
                });
        }


        UsherModeXFListenerDialog.IXunFeiResult xunFeiResult = new UsherModeXFListenerDialog.IXunFeiResult() {
                @Override
                public void onResult(String result) {
                        ettext2voice.append(result);
                }

                @Override
                public void onError(int result) {
                        ToastTools.newInstance(VipGuideTextToVoiceActivity.this).showText(getString(R.string.identification_error));
                }
        };

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }
}
